import { defineStore } from "pinia";
import store from "@/store/helper";
import { parse, stringify } from 'zipson'

const useCountStore = defineStore('counter', {
    state: () => ({
        count: 0,
        obj: {
            name: '张三',
            age: 18
        }
    }),
    actions: {
        increment() {
            this.count++
        },
        changeObj(obj: { name: string, age: number}) {
            if (obj.name === this.obj.name) {
                this.obj = {
                    name: '张三',
                    age: 18
                }
            } else {
                this.obj = obj
            }
            
        }
    },
    persist: {
        storage: sessionStorage,
        serializer: {
            deserialize: parse,
            serialize: stringify
        },
        pick: ['count', 'obj']
    }
})

// 在没有app.use(store)的地方使用,app.use的功能是通过inject函数注入store，
// 如果没有注入，就会报错，可以通过手动注入的方式，在外部使用
export function useCountStoreWithOut() {
    return useCountStore(store)
}

export default useCountStore